package view;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;


public class PopupEditInfo extends JDialog {

	private JLabel lastNameLabel, firstNameLabel;
	private JTextField lastNameField, firstNameField;
	private View laFenetre;
	
	public JTextField getLastNameField() {return lastNameField;}
	public JTextField getFirstNameField() {return firstNameField;}
	public View getLaFenetre() {return laFenetre;}
	
	
	public PopupEditInfo(JFrame parent){
	
		super(parent, "Edit info", true);
		this.laFenetre = (View) parent;
		this.setSize(200, 150);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setLayout(new GridLayout(4,2));

		
		lastNameLabel = new JLabel("Lastname : ");
		this.add(lastNameLabel);
		lastNameField = new JTextField();
		lastNameField.setText(this.laFenetre.getController().getModel().getProperties().getProperty("lastname"));
		this.add(lastNameField);
		
		firstNameLabel = new JLabel("Firstname : ");
		this.add(firstNameLabel);
		firstNameField = new JTextField();
		firstNameField.setText(this.laFenetre.getController().getModel().getProperties().getProperty("firstname"));
		this.add(firstNameField);
		
		JButton okBouton = new JButton("OK");
		okBouton.addActionListener(new MonControleurBoutonOK(this));
				
		
		JButton cancelBouton = new JButton("Annuler");
		cancelBouton.addActionListener(new MonControleurBoutonOK(this));
		this.add(okBouton);
		this.add(cancelBouton);
	}
	
}

class MonControleurBoutonOK implements ActionListener {
	private PopupEditInfo lepopup;
	
	MonControleurBoutonOK(PopupEditInfo arg1){	
		this.lepopup = arg1;
	}
	
	@Override
	public void actionPerformed(ActionEvent ae) {
		if (ae.getActionCommand() == "OK"){
		String [] param = {this.lepopup.getFirstNameField().getText(),
				this.lepopup.getLastNameField().getText()};
		
		
		this.lepopup.getLaFenetre().getController().updateInfo(param);
		
		}
		this.lepopup.setVisible(false);
	}
	
	
}

